<?php

/**
 *   商品套装相关函数库
 *   上海百胜软件有限公司
 *   网站地址: http://baison.com.cn;
 *   author:  fillbag  286116033
 */

if (!defined('IN_OS')){
    die('Access Denied');
}

/**
 * 商品套装活动状态（注意参数一定是原始信息）
 * @param   array   $group_pro    商品套装原始信息
 * @return  int
 */
function group_pro_status($group_pro){
    $now = gmtime();
    if ($group_pro['is_finished'] == 0)
    {
        if ($now < $group_pro['start_time'])
        {
            return 0; // 未开始
        }
        elseif ($now > $group_pro['end_time'])
        {
            return 2; // 已结束，未处理
        }
        else
        {
            return 1; // 进行中
        }
    }
    elseif ($group_pro['is_finished'] == 1)
    {
        return 2; // 已结束，未处理
    }
    else
    {
        return 3; // 已结束，已处理
    }
}

function group_pro_info($group_pro_id, $config = false){
	$group_pro_id = intval($group_pro_id);
    $sql = "SELECT * FROM " . $GLOBALS['os']->table('group_promotion') . " WHERE group_promotion_id = '$group_pro_id'";
    $group_pro = $GLOBALS['db']->getRow($sql);
    if (empty($group_pro)){
        return array();
    }    
    $group_pro['status_no'] = auction_status($group_pro);
    if ($config == true){

        $group_pro['start_time'] = local_date('Y-m-d H:i', $group_pro['start_time']);
        $group_pro['end_time'] = local_date('Y-m-d H:i', $group_pro['end_time']);
    }else{
        $group_pro['start_time'] = local_date($GLOBALS['_CFG']['time_format'], $group_pro['start_time']);
        $group_pro['end_time'] = local_date($GLOBALS['_CFG']['time_format'], $group_pro['end_time']);
    }
    $group_pro['add_time'] = local_date($GLOBALS['_CFG']['time_format'], $group_pro['add_time']);

    /* 查询已确认订单数 */
    $sql = "SELECT COUNT(*)" .
            " FROM " . $GLOBALS['os']->table('order_info') .
            " WHERE extension_code = 'group_promotion'" .
            " AND extension_id = '$group_pro_id'" .
            " AND order_status " . db_create_in(array(OS_CONFIRMED, OS_UNCONFIRMED));
    $group_pro['order_count'] = $GLOBALS['db']->getOne($sql);
    return $group_pro;	
}

/**
 * 获得指定商品套装的相册
 */
function get_group_pro_gallery($group_pro_id)
{
    $sql = 'SELECT img_id, img_url, thumb_url, img_desc' .
        ' FROM ' . $GLOBALS['os']->table('group_promotion_gallery') .
        " WHERE is_reveal=1 AND group_promotion_id = '$group_pro_id' LIMIT 5";
    $row = $GLOBALS['db']->getAll($sql);
    /* 格式化相册图片路径 */
    foreach($row as $key => $gallery_img)
    {
        $row[$key]['img_url'] = get_image_path($group_pro_id, $gallery_img['img_url'], false, 'group_promotion');
        $row[$key]['thumb_url'] = get_image_path($group_pro_id, $gallery_img['thumb_url'], true, 'group_promotion');
    }
    return $row;
}

function group_pro_goods_type($group_pro_id){
	static $item_list=NULL;
	if($item_list===NULL){
	    $sql = "SELECT t.goods_type_id,t.goods_type_name,t.goods_count,t.is_present,t.add_money,gp.price_type,gp.discount FROM ".
	           $GLOBALS['os']->table('group_promotion_goods_type')." t LEFT JOIN ".$GLOBALS['os']->table('group_promotion').
	           " gp ON t.group_promotion_id=gp.group_promotion_id WHERE t.group_promotion_id=" .intval($group_pro_id) . 
	           " ORDER BY t.goods_type_id DESC";
	    $res = $GLOBALS['db']->query($sql);   
	    $group_pro_r_count = 0;
	    if($res){
		    $item_list = array(); 
		    while ($_row_item = $GLOBALS['db']->fetchRow($res)){
				$sql = "SELECT g.goods_id,g.goods_name,g.shop_price,g.goods_thumb FROM " . $GLOBALS['os']->table('group_promotion_goods') .
				       " AS gpg LEFT JOIN ". $GLOBALS['os']->table('goods') . " AS g ON g.goods_id = gpg.goods_id WHERE gpg.group_promotion_id = ".
			       	   intval($group_pro_id)." AND gpg.goods_type_id = " . intval($_row_item['goods_type_id']) . " ORDER BY gpg.promotion_goods_id DESC";
				$g_res = $GLOBALS['db']->query($sql);  	
				while ($_g_row_item = $GLOBALS['db']->fetchRow($g_res)){
					    if($_row_item['price_type']==1){
					    	$goods_price = floatval($_row_item['add_money']/$_row_item['goods_count']);
					    }else{
					    	if($_row_item['is_present']==1){
					    		$goods_price = floatval($_row_item['add_money']/$_row_item['goods_count']);	
					    	}else{
					    		$goods_price = floatval($_g_row_item['shop_price']*($_row_item['discount']/100));
					    	}
					    }
					    $_g_row_item['goods_price'] =  price_format($goods_price, false); 
					    $_g_row_item['url']          =  build_uri('goods', array('gid' => $_g_row_item['goods_id']), $_g_row_item['goods_name']);
				        $_row_item['goods_list'][]	= $_g_row_item;
				}
				$_row_item['add_money_format']  = price_format($_row_item['add_money'], false);
				$_row_item['goods_list'] = sizeof($_row_item['goods_list'])>0 ? $_row_item['goods_list'] : array();
				$item_list[] = $_row_item;
				if($_row_item['is_present']==0){
					$group_pro_r_count += $_row_item['goods_count'];
				}
		    }
	    }
	}
    return array('list'=>$item_list,'group_pro_r_count'=>$group_pro_r_count);
}
function get_group_pro_goods_price($group_pro,$goods_type_id,$goods_id=0){
	global $db,$os;
    $gpg_type = $db->getRow("SELECT goods_type_id,goods_count,is_present,add_money FROM ".$os->table('group_promotion_goods_type').
	                       		" WHERE goods_type_id = ".intval($goods_type_id)." LIMIT 1");    	
	if($group_pro['price_type']==1){
	    $gpg_price = 	floatval($gpg_type['add_money'] / $gpg_type['goods_count']);
	}elseif($group_pro['price_type']==2){	
		if($gpg_type['is_present']==1){
			$gpg_price = floatval($gpg_type['add_money'] / $gpg_type['goods_count']);
		}else{	
	   		$g = $db->getRow("SELECT goods_id,shop_price FROM ".$os->table('goods')." WHERE goods_id = ".intval($goods_id)." LIMIT 1");   
	   		$gpg_price =  floatval($g['shop_price'] * ($group_pro['discount']/100));	
		}
	}    	
	return   $gpg_price;	
}
?>